package controller

import (
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"

	"cvevulner/cve-timed-task/deletetask"
	"cvevulner/cve-timed-task/emailtask"
	"cvevulner/cve-timed-task/gitwebtask"
	"cvevulner/cve-timed-task/tabletask"
	"cvevulner/cve-timed-task/util"
)

// GwController Grab warehouse yaml data
func GwController() {
	logs.Info("START TASK:", "Start of crawling organization, warehouse, owners correspondence relationship timing task")
	gitwebtask.Handle()
	//TODO
	logs.Info("END TASK:", "Grab warehouse yaml data task completed")
}

// RunTableTask Parse the artificial CVE table, crawl the CVE official
// website data, and store it in the database
func RunTableTask() {
	logs.Info("START TASK:", "Analyze the manual data table, crawl the CVE official website data, and start the timing task")
	tabletask.AddErrorDetails()
	tabletask.HandleDate()
	tabletask.RepeatTask()
	tabletask.GetPublishedDateTask()
	logs.Info("END TASK:", "Analyze the manual data table, crawl the CVE official website data task completed")
}

// ToExcelController Query the data whose cve_status status is 3 or 4 in the
// database to generate excel Modify the status of cve_status to 3 or 4 to 5,
// indicating that it has been added to excel to be processed
func ToExcelController() {
	logs.Info("START TASK:", "Export data to generate excel, send error data feedback email task start")
	filePath := beego.AppConfig.String("timedtask::packageDir") + "problemexcels/"
	err := util.MakeDir(filePath)
	if err != nil {
		logs.Error("util.makeDir error", err)
		return
	}
	createTime := time.Now().Format("2006-01-02")

	tabletask.GenerateTable(filePath + "error_data_" + createTime + ".xls")
	emailtask.SendingTypeTwo(filePath)
	logs.Info("END TASK:", "To excel and send email task completed")
}

// RunMappingTask Parse the artificial CVE table, crawl the CVE
// official website data, and store it in the database
func RunMappingTask() {
	logs.Info("START TASK:", "The timing task of parsing the package name mapping table starts")
	tabletask.ToMysql()
	logs.Info("END TASK:", "The timing task of parsing the package name mapping table completed")
}

// RunDelFileTask Delete files older than one month
func RunDelFileTask() {
	logs.Info("START TASK:", "Clean up the folder timed task starts")
	deletetask.DelDir(beego.AppConfig.String("timedtask::packageDir") + "oldexcels/")
	logs.Info("END TASK:", "Clean up the folder task completed")
}

// RunIssueRecordTask issues record email task
func RunIssueRecordTask() {
	logs.Info("START TASK:", "issues record email task start")
	filePath := beego.AppConfig.String("timedtask::packageDir") + "export_excels/"
	err := util.MakeDir(filePath)
	if err != nil {
		logs.Error("util.MakeDir error:", err, filePath)
		return
	}
	tabletask.GenerateExcels("1", filePath+"create_issues.xls")
	tabletask.GenerateExcels("2", filePath+"unctreated_issues.xls")
	emailtask.Sending(filePath)
	logs.Info("END TASK:", "issues record email task complete")
}

// RunWhitelistTask the task of parsing package whitelist
func RunWhitelistTask() {
	logs.Info("START TASK:", "the task of parsing package whitelist start")
	tabletask.ImportDataTypeTwo()
	logs.Info("END TASK:", "the task of parsing package whitelist complete")
}

// RunPackageCommitterTask the task of parsing package_committer excels
func RunPackageCommitterTask() {
	logs.Info("START TASK:", "the task of parsing package_committer_excels start")
	tabletask.ImportData()
	logs.Info("END TASK:", "the task of parsing package_committer_excels complete")
}

// SupplementCveTask Complete the template information of the issue with the data on the CVE official website
func SupplementCveTask() {
	logs.Info("START TASK:", "Supplemental cve information task starts")
	tabletask.SupplementCve()
	logs.Info("END TASK:", "Supplement cve information task is over")
}

// LongSupplementCveTask Complete the template information of the
// issue with the data on the CVE official website
func LongSupplementCveTask() {
	return
	logs.Info("START TASK:", "long Supplemental cve information task starts")
	tabletask.LongSupplementCve()
	logs.Info("END TASK:", "long Supplement cve information task is over")
}

// SyncPackagesTask sync package task.
func SyncPackagesTask() {
	logs.Info("START TASK:", "SyncPackagesTask task starts")
	tabletask.SyncPackages()
	logs.Info("END TASK:", "SyncPackagesTask task is over")
}
